<template>
  <view class="announcement-page">
    <!-- 搜索栏 -->
    <view class="search-section">
      <u-search 
        v-model="searchKeyword" 
        placeholder="搜索公告内容"
        :show-action="false"
        bg-color="#f5f5f5"
        @search="handleSearch"
        @clear="handleClear"
      ></u-search>
    </view>

    <!-- 分类筛选 -->
    <view class="filter-section">
      <scroll-view scroll-x class="filter-scroll">
        <view class="filter-list">
          <view 
            v-for="(item, index) in filterList" 
            :key="index"
            class="filter-item"
            :class="{ active: currentFilter === item.value }"
            @click="handleFilterChange(item.value)"
          >
            <text class="filter-text">{{ item.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 公告列表 -->
    <view class="announcement-list">
      <view 
        v-for="(item, index) in announcementList" 
        :key="item.id"
        class="announcement-item"
        @click="handleItemClick(item)"
      >
        <!-- 置顶标识 -->
        <view v-if="item.isTop" class="top-badge">
          <u-icon name="arrow-up" size="12" color="#fff"></u-icon>
          <text class="top-text">置顶</text>
        </view>

        <!-- 公告内容 -->
        <view class="item-content">
          <view class="item-header">
            <text class="item-title">{{ item.title }}</text>
          </view>
          <view class="item-status-row">
            <view class="item-status">
              <u-tag 
                :text="item.statusText" 
                :type="getStatusType(item.status)"
                size="mini"
              ></u-tag>
            </view>
          </view>
          
          <view class="item-body">
            <text class="item-summary">{{ item.summary }}</text>
          </view>
          
          <view class="item-footer">
            <view class="item-meta">
              <u-icon name="calendar" size="12" color="#999"></u-icon>
              <text class="meta-text">{{ item.publishTime }}</text>
            </view>
            <view class="item-meta">
              <u-icon name="eye" size="12" color="#999"></u-icon>
              <text class="meta-text">{{ item.viewCount }}人已读</text>
            </view>
            <view class="item-meta">
              <u-icon name="user" size="12" color="#999"></u-icon>
              <text class="meta-text">{{ item.publisher }}</text>
            </view>
          </view>
        </view>

        <!-- 箭头图标 -->
        <view class="item-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="announcementList.length === 0" class="empty-state">
      <u-icon name="file-text" size="48" color="#ccc"></u-icon>
      <text class="empty-text">暂无公告信息</text>
    </view>

    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more">
      <u-loading-icon v-if="loading" mode="circle"></u-loading-icon>
      <text v-else class="load-text" @click="loadMore">点击加载更多</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

// 搜索关键词
const searchKeyword = ref('')

// 当前筛选条件
const currentFilter = ref('all')

// 筛选列表
const filterList = reactive([
  { label: '全部', value: 'all' },
  { label: '重要通知', value: 'important' },
  { label: '活动公告', value: 'activity' },
  { label: '服务通知', value: 'service' },
  { label: '紧急通知', value: 'urgent' }
])

// 公告列表数据
const announcementList = ref([
  {
    id: 1,
    title: '关于小区电梯维修的通知',
    summary: '为了保障居民出行安全，物业公司将于本周六对1-3号楼电梯进行例行维护保养，预计维修时间为上午8:00-12:00...',
    status: 1,
    statusText: '进行中',
    isTop: true,
    publishTime: '2025-01-21 10:00',
    viewCount: 1256,
    publisher: '物业服务中心',
    category: 'important'
  },
  {
    id: 2,
    title: '春节期间物业服务安排',
    summary: '春节期间（2月10日-2月17日）物业服务安排如下：客服中心正常营业，维修服务24小时值班，垃圾清运时间调整...',
    status: 1,
    statusText: '进行中',
    isTop: false,
    publishTime: '2025-01-20 14:30',
    viewCount: 892,
    publisher: '物业服务中心',
    category: 'service'
  },
  {
    id: 3,
    title: '小区环境整治活动通知',
    summary: '为营造良好的居住环境，社区将于本周末开展环境整治活动，欢迎广大居民积极参与，共同维护小区环境...',
    status: 1,
    statusText: '进行中',
    isTop: false,
    publishTime: '2025-01-19 09:15',
    viewCount: 634,
    publisher: '社区居委会',
    category: 'activity'
  },
  {
    id: 4,
    title: '停车费缴费提醒',
    summary: '请各位车主及时缴纳2025年1月份停车费，缴费截止日期为1月31日，逾期将产生滞纳金...',
    status: 2,
    statusText: '已结束',
    isTop: false,
    publishTime: '2025-01-18 16:45',
    viewCount: 445,
    publisher: '停车管理部',
    category: 'service'
  },
  {
    id: 5,
    title: '社区春节联欢晚会报名通知',
    summary: '社区将于2月8日晚举办春节联欢晚会，现开始报名，欢迎有才艺的居民积极参与，报名截止日期为2月5日...',
    status: 1,
    statusText: '进行中',
    isTop: false,
    publishTime: '2025-01-17 11:20',
    viewCount: 789,
    publisher: '社区居委会',
    category: 'activity'
  }
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 获取状态类型
const getStatusType = (status: number) => {
  switch (status) {
    case 1: return 'primary'
    case 2: return 'info'
    case 3: return 'warning'
    default: return 'info'
  }
}

// 搜索处理
const handleSearch = (value: string) => {
  console.log('搜索公告:', value)
  // 这里实现搜索逻辑
}

// 清空搜索
const handleClear = () => {
  searchKeyword.value = ''
  console.log('清空搜索')
}

// 筛选条件改变
const handleFilterChange = (value: string) => {
  currentFilter.value = value
  console.log('筛选条件改变:', value)
  // 这里实现筛选逻辑
}

// 点击公告项
const handleItemClick = (item: any) => {
  console.log('点击公告:', item)
  // 跳转到公告详情页面
  uni.navigateTo({
    url: `/pages/community-announcement/detail?id=${item.id}`
  })
}

// 加载更多
const loadMore = () => {
  if (loading.value) return
  
  loading.value = true
  setTimeout(() => {
    // 模拟加载更多数据
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  console.log('社区公告页面加载完成')
})
</script>

<style lang="scss" scoped>
.announcement-page {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 0 0 20rpx 0;
}

.page-header {
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  padding: 20rpx 30rpx 30rpx;
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .header-title {
      .title-text {
        display: block;
        font-size: 36rpx;
        font-weight: 600;
        color: #fff;
        margin-bottom: 8rpx;
      }
      
      .title-subtitle {
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.8);
      }
    }
    
    .header-icon {
      width: 80rpx;
      height: 80rpx;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.search-section {
  padding: 24rpx 32rpx;
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.filter-section {
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  
  .filter-scroll {
    white-space: nowrap;
    padding: 0 32rpx 24rpx 32rpx;
    
    .filter-list {
      display: flex;
      padding: 24rpx 0 0 0;
      
      .filter-item {
        flex-shrink: 0;
        padding: 16rpx 28rpx;
        margin-right: 20rpx;
        border-radius: 24rpx;
        background-color: #f8f9fa;
        transition: all 0.3s ease;
        border: 2rpx solid transparent;
        
        &.active {
          background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
          transform: translateY(-2rpx);
          box-shadow: 0 4rpx 12rpx rgba(24, 144, 255, 0.25);
          
          .filter-text {
            color: #fff;
          }
        }
        
        .filter-text {
          font-size: 28rpx;
          color: #666;
          font-weight: 500;
          white-space: nowrap;
        }
      }
    }
  }
}

.announcement-list {
  padding: 0 32rpx;
  
  .announcement-item {
    background-color: #fff;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    padding: 32rpx;
    position: relative;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    border: 1rpx solid rgba(0, 0, 0, 0.04);
    
    &:active {
      transform: translateY(-2rpx);
      box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
    }
    
    .top-badge {
      position: absolute;
      top: 20rpx;
      right: 20rpx;
      background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
      color: #fff;
      padding: 8rpx 16rpx;
      border-radius: 20rpx;
      display: flex;
      align-items: center;
      font-size: 20rpx;
      
      .top-text {
        margin-left: 4rpx;
      }
    }
    
    .item-content {
      .item-header {
        margin-bottom: 16rpx;
        
        .item-title {
          font-size: 32rpx;
          font-weight: 600;
          color: #333;
          line-height: 1.4;
          word-break: break-all;
        }
      }
      
      .item-status-row {
        display: flex;
        justify-content: flex-end;
        margin-bottom: 20rpx;
        
        .item-status {
          flex-shrink: 0;
        }
      }
      
      .item-body {
        margin-bottom: 20rpx;
        padding-right: 20rpx;
        
        .item-summary {
          font-size: 28rpx;
          color: #666;
          line-height: 1.5;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          word-break: break-all;
        }
      }
      
      .item-footer {
        display: flex;
        align-items: center;
        gap: 30rpx;
        
        .item-meta {
          display: flex;
          align-items: center;
          gap: 8rpx;
          
          .meta-text {
            font-size: 24rpx;
            color: #999;
          }
        }
      }
    }
    
    .item-arrow {
      position: absolute;
      right: 30rpx;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-text {
    margin-top: 20rpx;
    font-size: 28rpx;
    color: #999;
  }
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx 0;
  
  .load-text {
    font-size: 28rpx;
    color: #1890ff;
  }
}
</style>
